<?php
include '../include/functions.php';
//set session save path
  session_save_path("../$session_path");
  // Initialize a session. 
  session_start(  );
            //session_destroy();             
  $count++;                         
  $sessionId = session_id();
//Connect To Database
dbConnect();
if($userID != "")
{
	$SQL = "SELECT Username FROM Users WHERE ID=$userID";
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	$myrow = mysql_fetch_array($result);
	$username = $myrow["Username"];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//Dtd html 4.01 Transitional//EN"
"http://www.w3.org/tr/html4/loose.dtd">
<html>
  <head>
    <title>JoshHughes.com Hockey Pool</title>
	<link rel="stylesheet" href="http://hockey.joshhughes.com/css/hockey.css" type="text/css">
    <script src="http://hockey.joshhughes.com/scripts/prototype.js" type="text/javascript"></script>
	<script src="http://hockey.joshhughes.com/scripts/scriptaculous.js" type="text/javascript"></script>
	<script src="http://hockey/joshhughes.com/scripts/hockey.js" type="text/javascript"></script>  </head>
<body>
<div id="pagewidth" >
	<div id="header" >
	
		<?PHP include_once("../layout/header.html"); ?>
		
	</div>
			<div id="wrapper" class="clearfix" > 
			<div id="maincol" >

			<?php
				
				$js="\nfunction smilie(emote) {\n  document.topic.comments.value += emote;\n  document.topic.comments.focus();\n}\n";
				
				if (isset($_POST['comments'])) {
				  if (strlen($_POST[comments]) == 0) {
					require_once("headers.php");
					echo "<p><b>You have submitted an empty post.</b></p>\n<p>Please <a href=\"javascript:history.go(-1)\">Go back</a> and try again.</p>\n";
					require_once("footers.php");
				  }
				  else {
					require_once("config.inc.php");
					require_once("mysql.php");
					require_once("tags.php");
					require_once("censorship.php");
					$comments=strip_tags($_POST[comments],$allowedtags);
					foreach ($banlist as $word) {
					  $count=strlen($word);
					  $i=0;
					  while ($i < $count) {
						$replace .= "*";
						$i++;
					  }
					  $comments=str_replace($word,$replace,$comments);
					  unset($replace,$count);
					}
					$date=time();
					$Username=$username;
					$sql="INSERT INTO tinybb_posts SET topicid='$_POST[id]', author='$Username', date='$date', text='$comments'";
					mysql_query($sql);
					$postid=mysql_insert_id();
					$sql_topic="UPDATE tinybb_topics SET lastpost='$date', lastpostid='$postid' WHERE id='$_POST[id]'";
					mysql_query($sql_topic);
					?>
					<SCRIPT LANGUAGE="JavaScript">window.location="topic.php?id=<?= $_POST[id]; ?>";</script>
					<?
				  }
				}
				require_once("headers.php");
				if (isset($_GET['id'])) {
				  $count=mysql_result(mysql_query("SELECT count(*) FROM tinybb_topics WHERE id='$_GET[id]'"),0);
				  if ($count == 1) {
					$topicname=mysql_result(mysql_query("SELECT name FROM tinybb_topics WHERE id='$_GET[id]'"),0);
					echo "<a href=\"rss/posts.php?id=$_GET[id]\" target=\"_blank\"> <img border=\"0\" src=\"_images/valid_rss.png\" width=\"85\" height=\"15\" align=\"right\" alt=\"XML RSS Feed\" /> </a>";
					echo "<span style=\"float: right;\">Add this topic to your favorite RSS feed reader! &nbsp;</span>";
					echo "<h1>$topicname</h1><hr style=\"border: none 0; border-top: 1px solid #346FB7; height: 3px; width: 80%; text-align: left; margin: 0 auto 0 0;\" />\n";
					$sql="SELECT * FROM tinybb_posts WHERE topicid='$_GET[id]' ORDER BY date";
					$result=mysql_query($sql);
					while ($row = mysql_fetch_array($result)) {
					  $time=date("H:i.s",$row[date]);
					  $date=date("D jS M Y",$row[date]);
					  $author=$row[author];
					  $text=str_replace("\r","",$row[text]);
					  $text=str_replace("\n","<br />",$text);
					  $lasthour = time()-3601;
					  foreach ($smilies as $emote) {
						 $text = str_replace(" [$emote] "," <img border=\"0\" src=\"_images/smilies/$emote.gif\" alt=\"$emote\" width=\"16px\" height=\"16px\" /> ",$text);
					  }
					  echo "<p><span style=\"font-size:85%;\"><b class=\"author\">$author</b> | $time | $date";
					  $moderaters=array($tinybb_moderators);
					  if (in_array($username,$moderaters)) {
						echo " | <a href=\"edit_mod.php?id=$row[id]\" class=\"permLink\">Edit</a> | <a href=\"edit_del.php?id=$row[id]\" class=\"permLink\">Del</a>";
					  }
					  elseif (($username == $author) AND ($row[date] > $lasthour)) {
						echo " | <a href=\"edit.php?id=$row[id]\" class=\"permLink\">Edit</a>";
					  }
					  echo "</span><br /><style=\"font-size:105%;\">$text</span><br /><br /></p>\n";
					}
				
					if ($username != "") {
					  echo "<a name=\"post\"></a>
				<form name=\"topic\" action=\"topic.php\" method=\"post\">
					<p>
						<hr style=\"border: none 0; border-top: 1px solid #346FB7; height: 3px; width: 80%; text-align: left; margin: 0 auto 0 0;\" />
						<b class=\"nonpost\"><label for=\"comments\">Post a message:</label></b><br />
						The only allowed html tags are $allowedtagshtml
						<br /><br /><b class=\"nonpost\">Add smilies:</b><br />\n";
					  foreach ($smilies as $emote) {
						echo "		<a href=\"#post\" onclick=\"smilie(' [$emote] '); return false;\"><img border=\"0\" src=\"_images/smilies/$emote.gif\" alt=\"$emote\" /></a>&nbsp; \n";
					  }
					  echo "	</p>
					<textarea name=\"comments\" id=\"comments\" cols=\"52\" rows=\"8\">$comments</textarea><br /><br />
					<input type=\"hidden\" name=\"id\" value=\"$_GET[id]\" />
					<input type=\"image\" class=\"clear\" src=\"_images/form_submit.gif\" />
					<p>You can edit this post for up to 1 hour after it has been submitted.</p>
				</form>\n";
					}
					else {
					  echo "-$username-<p>Please <b class=\"author\">log in</b> at the top left or <a href=\"../register.php\" class=\"permLink\">register</a> to post on the forum:</b></p>\n";
					}
				  }
				}
				
				require_once("footers.php");
				?>
			
			</div>
			<div id="leftcol" >
			
				<?PHP include_once("../layout/left_column.php"); ?>
		
			</div>
			
		</div>
		
		<div id="footer" >
		
			<?PHP include_once("../layout/footer.html"); ?>
			
		</div>
</div>
</body>
</html>